<template>
    <div>
        <el-breadcrumb separator="/">
            <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>权限管理</el-breadcrumb-item>
            <el-breadcrumb-item>角色列表</el-breadcrumb-item>
        </el-breadcrumb>
        <el-card>
            <el-button type="primary">添加角色</el-button>
            <el-table
                :data="rolesList"
                border stripe
                style="width: 100%">
                <el-table-column
                    label="#"
                    type="expand">
                    <template slot-scope="scope">
                        <el-row :class="['bdBottom','centerIa',index===0?'bdTop':'']" v-for="(item,index) in scope.row.children" :key="index">
                            <el-col :span="5">
                                <el-tag closable @close="deleteRoleById(scope.row,item.id)">{{item.authName}}</el-tag>
                                <i class="el-icon-caret-right"></i>
                            </el-col>
                            <el-col :span="19">
                                <el-row :class="['bdTop','centerIa',i==0?'bdNo':'']" v-for="(v,i) in item.children" :key="i">
                                    <el-col :span="6">
                                        <el-tag closable type="success" @close="deleteRoleById(scope.row,v.id)">{{v.authName}}</el-tag>
                                        <i class="el-icon-caret-right"></i>
                                    </el-col>
                                    <el-col :span="18" >
                                        <el-tag closable type="warning" @close="deleteRoleById(scope.row,k.id)" v-for="(k,j) in v.children" :key="j">{{k.authName}}</el-tag>
                                    </el-col>
                                </el-row>
                            </el-col>
                        </el-row>
                    </template>
                </el-table-column>
                <el-table-column
                    prop="roleName"
                    label="角色名称">
                </el-table-column>
                <el-table-column
                    prop="roleDesc"
                    label="角色描述">
                </el-table-column>
                <el-table-column
                    width="300px"
                    label="操作">
                    <template slot-scope="scope">
                        <el-button type="primary" size="mini" icon="el-icon-edit" @click="eidtRoles(scope.row.id)">编辑</el-button>
                        <el-button type="danger" size="mini" icon="el-icon-delete" @click="deleteRolesById(scope.row.id)">删除</el-button>
                        <el-button type="warning " size="mini" icon="el-icon-setting" @click="assignAuthority(scope.row)">分配权限</el-button>
                    </template>
                </el-table-column>
            </el-table>
        </el-card>

        <el-dialog
            title="权限列表"
            :visible.sync="rolesVisible"
            width="50%"
            @close="closeRolesVisible"
            >
            <el-tree
                :data="rightsList"
                show-checkbox
                node-key="id"
                default-expand-all
                :default-checked-keys="defaultKeys"
                ref="rolesRef"
                :props="treeProps">
            </el-tree>
            <span slot="footer" class="dialog-footer">
                <el-button @click="rolesVisible = false">取 消</el-button>
                <el-button type="primary" @click="addRoles">确 定</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script lang="ts">
    import {apiAddress} from "@/utils/apiAddress"
    import {Component,Vue} from "vue-property-decorator";
    @Component({})
    export default class Roles extends Vue{
        rolesList:[]=[]
        rolesVisible:boolean= false
        rightsList:[]=[]
        treeProps:{
            children:String,
            label:String
        }={
            children: 'children',
            label: 'authName'
        }
        defaultKeys:[]=[]
        roleId:Number=0
        created() {
            this.getRoles()
        }

        closeRolesVisible(){
            this.defaultKeys=[]
        }
        async addRoles(){
            const arr=[
                ...(this.$refs.rolesRef as any).getCheckedKeys(),
                ...(this.$refs.rolesRef as any).getHalfCheckedKeys()
            ]
            
            const rids=arr.join(",");
            const data={
                roleId:this.roleId,
                rids:rids
            }
            const res=await apiAddress.addRoles(data);
            if(res.meta.status!=200) return this.$message.error(res.meta.msg);
            this.rolesVisible=false;
            this.getRoles()
        }
        async assignAuthority(roles:any){
            this.roleId=roles.id
            const res=await apiAddress.getRightsList({type:'tree'});
            if(res.meta.status!=200) return this.$message.error(res.meta.msg);
            this.rightsList=res.data
            this.getDefaultKeys(roles,this.defaultKeys)
            this.rolesVisible=true
        }
        eidtRoles(){}
        deleteRolesById(){}
        async deleteRoleById(row:any,id:Number){
            const confirm= await this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).catch(error=>error)

            if(confirm!="confirm") return this.$message.info("取消删除");
            const res= await apiAddress.deleteRoleById({roleId:row.id,rightId:id});
            if(res.meta.status!=200) return this.$message.error(res.meta.msg);
            this.$message.success(res.meta.msg);
            row.children=res.data
        }
        getDefaultKeys(node:any,arr:Array<any>){
            if(!node.children){
                return arr.push(node.id)
            }
            node.children.forEach((item:any)=>{
                this.getDefaultKeys(item,arr)
            })
        }
        async getRoles() {
            const res=await apiAddress.getRoles({});
            if(res.meta.status!=200) return this.$message.error(res.meta.msg);
            this.rolesList=res.data
        }
    }
</script>

<style scoped>
.el-tag{
    margin: 7px;
}
.bdTop{
    border-top: solid 1px #eee;
}
.bdBottom{
    border-bottom: solid 1px #eee;
}
.bdNo{
    border: 0;
}
.centerIa{
    display: flex;
    align-items: center;
}
</style>